package uiComponents;


import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.plaf.ButtonUI;
import javax.swing.text.IconView;

import core.FileOpen;
import core.FileSave;
import core.WindowManager;

import cPanes.CPane;
import cPanes.DrawingPane;
import cPanes.EquPane;
import cPanes.OrgPane;
import cPanes.TextPane;




/**
 * A window, which contains an InfScrollPane. It also has a JMenuBar which provides file-wide functionality.
 */
public class MainWindow extends JFrame implements ActionListener {
	private JMenuBar topbar = new JMenuBar();
	private JMenuItem[] addables;
	private ArrayList<CPane> contents;
	private JMenu add;
	private JMenu file;
	private InfScrollPane pa;
	private JPanel p;
	private JMenuItem[] fileops;
	private JButton paste;
	private JButton help;
	private WindowManager parent;
	private JFileChooser fchoose;
	
	/**
	 * Sets up the basic window functions, and instantiates the content pane.
	 * @param m The WindowManager which owns this window
	 * @param name The index of this window in the WindowManager m
	 */
	public MainWindow(WindowManager m, String name){
		parent = m;
		pa = new InfScrollPane();
		add(pa);
		p= pa.getContentPane();
		contents=new ArrayList();
		setTitle(name);
		setSize(500,500);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		menuSetup();
		setVisible(true);
		
	}
	/**
	 * Gets the WindowManager that owns this window.
	 * @return The WindowManager that owns this window
	 */
	public WindowManager getManager(){
		return parent;
	}
	/**
	 * Gets the content pane for this window.
	 * @return The InfScrollPane that displays this window's contents.
	 */
	public JPanel getScrollPane(){
		return p;
	}
	/**
	 * Creates and populates the menus for the topbar.
	 */
	private void menuSetup(){
		//Create the menus
		add = new JMenu("Add");
		file = new JMenu("File");
		paste = new JButton("Paste");
		paste.setBackground(add.getBackground());
		paste.setBorder(null);
		paste.setContentAreaFilled(false);
		paste.addActionListener(this);
		help = new JButton("Help");
		help.setContentAreaFilled(false);
		help.setBackground(add.getBackground());
		help.setBorder(null);
		help.addActionListener(this);
		fileops = new JMenuItem[3];
		fileops[0]= new JMenuItem("New");
		fileops[1]= new JMenuItem("Save");
		fileops[2]= new JMenuItem("Open");
	
		for (int i=0; i<fileops.length; i++){
			fileops[i].addActionListener(this);
			file.add(fileops[i]);
		}
		//Modify this value and add menu items to add new classes
		addables= new JMenuItem[4];
		addables[0] = new JMenuItem("OrgPane");
		addables[1] = new JMenuItem("TextPane");
		addables[2] = new JMenuItem("DrawPane");
		addables[3] = new JMenuItem("EquPane");
		for (int i=0; i<addables.length; i++){
			addables[i].addActionListener(this);
			add.add(addables[i]);
		}
		//Add all the elements to the topbar
		topbar.add(file);
		topbar.add(add);
		topbar.add(paste);
		topbar.add(Box.createRigidArea(new Dimension(10,10)));
		topbar.add(help);
		topbar.setVisible(true);
		this.setJMenuBar(topbar);
	}
	/**
	 * Handles the events generated by the MenuBar.
	 */
	public void actionPerformed(ActionEvent arg0){
		Object s = arg0.getSource();
		if(s.equals(fileops[0])){
			parent.newBlankWindow();
		}
		else if(s.equals(fileops[1])){
			saveFile();
		}
		else if(s.equals(fileops[2])){
			openFile();
		}
		else if(s.equals(addables[0])){
			OrgPane t=new OrgPane();
			add(t);
		}
		else if(s.equals(addables[1])){
			TextPane t=new TextPane();
			add(t);
		}
		else if(s.equals(addables[2])){
			DrawingPane t=new DrawingPane();
			add(t);
		}
		else if(s.equals(addables[3])){
			EquPane t=new EquPane();
			add(t);
		}
		else if(s.equals(paste)){
			paste();
		}
		else if (s.equals(help)){
			JOptionPane.showMessageDialog(this, "ALT-click to move objects, ALT-click their borders to resize \nCTRL-click for a context menu", "Help", JOptionPane.PLAIN_MESSAGE);
		}
	}
	/**
	 * Add a CPane. This puts the selected CPane on top of the ZOrder, and triggers a repaint.
	 * @param c The CPane to add.
	 */
	public void add(CPane c){
		p.add(c);
		contents.add(c);
		c.moveToTop();
		this.repaint();
	}
	
	/**
	 * Opens a file-save dialog, gets the result and puts a copy of this document there.
	 */
	private void saveFile(){
		fchoose=new JFileChooser();
		fchoose.addActionListener(this);
		int x = fchoose.showSaveDialog(this);
		if (x==JFileChooser.APPROVE_OPTION){
			CPane[] t = new CPane[0];
			FileSave save = new FileSave(contents.toArray(t), fchoose.getSelectedFile());
			save.start();
		}
	}
	
	/**
	 * Opens a file-open dialog, gets the result and opens the document there
	 */
	private void openFile(){
		fchoose=new JFileChooser();
		fchoose.addActionListener(this);
		int x=fchoose.showOpenDialog(this);
		if(x==JFileChooser.APPROVE_OPTION){
			FileOpen open = new FileOpen(fchoose.getSelectedFile(), parent);
			open.start();
		}
	}
	
	/**
	 * Grabs the current clipboard contents, if any, staggers it and prints it.
	 */
	private void paste(){
		if (getManager().getClipboard()!=null){
			CPane c = getManager().getClipboard();
			c.setX(c.getX()+15);
			c.setY(c.getY()+15);
			add(c);
		}
	}
}
